package com.cdd.manage.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.manage.mapper.SendmsgBusinessMapper;
import com.cdd.manage.openFeign.ClientFeign;
import com.cdd.manage.service.SendmsgBusinessService;
import com.cdd.manage.vo.ClientVo;
import com.cdd.manage.vo.SendmsgBusinessVo;
import com.common.entity.SendmsgBusiness;
import com.common.untils.CommonResult;
import com.sun.corba.se.spi.orbutil.threadpool.WorkQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author lenovo
 * @version 1.0
 * @since 2022-09-06  17:16:59
 */
@Service
public class SendmsgBusinessServiceImp implements SendmsgBusinessService {
    @Autowired
    private SendmsgBusinessMapper sendmsgBusinessMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ClientFeign clientFeign;
    //查询所有加分页
    @Override
    public CommonResult selectAll(Integer currentPage, Integer pageSize, SendmsgBusinessVo sendmsgBusinessVo,String token) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String userId = user.getString("userId");
        if (userId.equals(123)){
            Page<SendmsgBusiness> page=new Page(currentPage,pageSize);
            List<Map> allClientNameAndId = clientFeign.findAllClientNameAndId();
            QueryWrapper<SendmsgBusiness> wrapper=new QueryWrapper();
            wrapper.eq("s.is_deleted",0);
            if (StringUtils.hasText(sendmsgBusinessVo.getGmtCreate())) {
                wrapper.ge("s.gmt_create",sendmsgBusinessVo.getGmtCreate());
            }
            if (StringUtils.hasText(sendmsgBusinessVo.getGmtModified())) {
                wrapper.le("s.gmt_create",sendmsgBusinessVo.getGmtModified());
            }
            IPage<Map> iPage=sendmsgBusinessMapper.selectAll(page,wrapper);
            Map map1=new HashMap();
            map1.put("ipage",iPage);
            map1.put("allClientNameAndId",allClientNameAndId);
            return new CommonResult(2000,"查询成功",map1);
        }else {
            Page<SendmsgBusiness> page=new Page(currentPage,pageSize);
            List<Map> allClientNameAndId = clientFeign.findAllClientNameAndId();
            QueryWrapper<SendmsgBusiness> wrapper=new QueryWrapper();
            wrapper.eq("s.is_deleted",0);
            wrapper.eq("s.user_id",userId);
            if (StringUtils.hasText(sendmsgBusinessVo.getGmtCreate())) {
                wrapper.ge("s.gmt_create",sendmsgBusinessVo.getGmtCreate());
            }
            if (StringUtils.hasText(sendmsgBusinessVo.getGmtModified())) {
                wrapper.le("s.gmt_create",sendmsgBusinessVo.getGmtModified());
            }
            IPage<Map> iPage=sendmsgBusinessMapper.select(page,wrapper,userId);
            Map map1=new HashMap();
            map1.put("ipage",iPage);
            map1.put("allClientNameAndId",allClientNameAndId);
            return new CommonResult(2000,"查询成功",map1);
        }

    }
    //添加
    @Override
    public CommonResult insert(SendmsgBusiness sendmsgBusiness, String token) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String userId = user.getString("userId");
        //获取员工的id
        sendmsgBusiness.setUserId(user.getString("userId"));
        //添加
        sendmsgBusiness.setMsg(sendmsgBusiness.getMsg());
        sendmsgBusiness.setStart(0);
        sendmsgBusiness.setIsDeleted(0);
        Integer row = sendmsgBusinessMapper.insert(sendmsgBusiness);
        return new CommonResult(2000,"添加成功",null);
    }
    //查询发布的内容
    @Override
    public CommonResult query(Integer id) {
        QueryWrapper<SendmsgBusiness> wrapper =new QueryWrapper();
        wrapper.eq("id",id);
        wrapper.eq("is_deleted",0);
        SendmsgBusiness sendmsgBusiness = sendmsgBusinessMapper.selectOne(wrapper);
        return new CommonResult(2000,"查看内容成功",sendmsgBusiness);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        QueryWrapper<SendmsgBusiness> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        int delete = sendmsgBusinessMapper.delete(wrapper);
        if (delete==1){
            return new CommonResult(2000,"删除成功",null);
        }
        return new CommonResult(5000,"删除失败",null);
    }
    //前端通知
    @Override
    public CommonResult manageAllById(Integer currentPage, Integer pageSize,String hometoken) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(hometoken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        String bid = client.getString("bid");
        Page<SendmsgBusiness> page=new Page<>(currentPage,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("s.is_deleted",0);
        wrapper.eq("b_id",bid);
        IPage iPage = sendmsgBusinessMapper.manageAllById(page, wrapper);
        return new CommonResult(2000,"查询成功",iPage);
    }

    @Override
    public CommonResult updateManage(Integer id) {
        QueryWrapper<SendmsgBusiness> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        SendmsgBusiness sendmsgBusiness=new SendmsgBusiness();
        sendmsgBusiness.setStart(1);
        int update = sendmsgBusinessMapper.update(sendmsgBusiness, wrapper);
        if (update==1){
            int delete = sendmsgBusinessMapper.delete(wrapper);
            if (delete==1){
                return new CommonResult(2000,"修改成功",null);
            }else {
                return new CommonResult(5000,"修改失败",null);
            }
        }
        return new CommonResult(5001,"修改失败",null);
    }

}
